%%%%%% This code is to find the effects of banks' upstream market power in Table 7 
% last date of modification:1/15/2023
% modification: change the average mortgage size in different market to referee's comments 
clear
cd ('F:\home\Dropbox\Mortgage_company\JobMarketProject\Replication_Package\financing_competitors_upload\code\');

%% market power 
rno = 4.52;
rnb = 4.45;
rb = 4.4;
rm = 4.25
outside = 1-0.2-0.24-0.18-0.05;

sigma = 0.3
F= 15754*354500 % mean number of potential buyers * average mortgage size 
alpha = 1.14;
qb = 2.43; 
qn = 1.67;
k = 3.21;
psim = 3.5;
psiw = 3.28;
eta = 0.09;
rhob = 3.69;
rhoo = 4.38;
%% find mean, top decile and bottom decile 
player_5 = [11;7;6]; % nLS, nNLS, nMB, nFB
player_50 = [24;11;8]; % mean 
player_95 = [44;12;10];

%% Quantification: Effect of Banks' Market Power (What if banks are perfectly substitutable?)
% Baseline analysis
[rno1,sno1,rnb1,snb1,rm1,sbm1,rb1,sb1, rhob1, sbw1, sow1,markup11,markup21] = Solve_local(F,sigma,psim,psiw,alpha,k,player_50(3),player_50(1),player_50(2),qn,qb,eta,rhoo);

markup_percent = (rhob-psiw)/psiw % 12.5%

mortgage_markup_m = (rm-psim)/psim % 21.4%
mortgage_markup_w = (rb-psim)/psim % 25.7%
%
%% Perfect substitutes case
sigma2=38;
[rno2,sno2,rnb2,snb2,rm2,sbm2,rb2,sb2, rhob2, sbw2, sow2,markup12,markup22] = Solve_local(F,sigma2,psim,psiw,alpha,k,player_50(3),player_50(1),player_50(2),qn,qb,eta,rhoo);

% warehouse lending rate 
markup1 = rhob1-psiw
markup2 = rhob2-psiw
d_rhob = rhob2-rhob1 % 38.86bps
(rhob2-rhob1)/rhob1 % 10.6%

% mortgage rate
ave_rn01 = (rnb1*snb1*sbw1*player_50(3)*player_50(1)+rno1*sno1*sow1*player_50(1))/(snb1*sbw1*player_50(3)*player_50(1)+sno1*sow1*player_50(1));
ave_rn1 = (rnb2*snb2*sbw2*player_50(3)*player_50(1)+rno2*sno2*sow2*player_50(1))/(snb2*sbw2*player_50(3)*player_50(1)+sno2*sow2*player_50(1));
diff1_rn = ave_rn01-ave_rn1 %4.1bps
(ave_rn01-ave_rn1)/ave_rn1 % 93bps

% Intermediated by shadow bank
shadow_share1 = sno1*player_50(1)*sow1+snb1*player_50(1)*sbw1*player_50(3)
shadow_share2 = sno2*player_50(1)*sow2+snb2*player_50(1)*sbw2*player_50(3)
ds_volume = (shadow_share1-shadow_share2)*F % -49.4M
(shadow_share1-shadow_share2)/shadow_share2 %3.4%
% Intermediated by bank
bank_share1 = sb1*player_50(3)+sbm1*player_50(2)
bank_share2 = sb2*player_50(3)+sbm2*player_50(2)
db_volume = (bank_share1-bank_share2) %51bps

% Volume
outside1 = 1-shadow_share1-bank_share1
outside2 = 1-shadow_share2-bank_share2
d_credit = (shadow_share1+bank_share1-shadow_share2-bank_share2)*F % average MSA: -21.0M in an average MSA
(shadow_share1+bank_share1-shadow_share2-bank_share2)/(shadow_share2+bank_share2) % -54bps


% consumer surplus 
annuity_factor = (1-(1+0.04)^(-3))/0.04
surplus1 =0.01*F*annuity_factor*( (log(player_50(3)*exp(-alpha*rb1+qb)+player_50(2)*exp(-alpha*rm1+qb)+player_50(1)*sow1*exp(-alpha*rno1+qn)+player_50(1)*sbw1*player_50(1)*exp(-alpha*rnb1+qn))/alpha))
surplus2 =0.01*F*annuity_factor*( (log(player_50(3)*exp(-alpha*rb2+qb)+player_50(2)*exp(-alpha*rm2+qb)+player_50(1)*sow2*exp(-alpha*rno2+qn)+player_50(1)*sbw2*player_50(1)*exp(-alpha*rnb2+qn))/alpha))

d_surplus = surplus1-surplus2 % -10.0M
d_surplus/(0.01*F) % -0.18


%% Consumer surplus: concentration and shadow banks' service quality 
clear
rno = 4.52;
rnb = 4.45;
rb = 4.4;
rm = 4.25
outside = 1-0.2-0.24-0.18-0.05;

sigma = 0.3
alpha = 1.14;
qb = 2.43; 
qn = 1.67;
kappa = 3.21;
k = 3.21;
psim = 3.5;
psiw = 3.28;
eta = 0.09;
rhob = 3.69;
rhoo = 4.38;

player_5 = [11;7;6]; % nLS, nNLS, nMB, nFB
player_50 = [24;11;8]; % mean 
player_95 = [44;12;10];
%% Most concentrated 
%F= 7017*354500 % potential buyers * average mortgage size 
F = 7017*214000 % million
F_tot_50 = 15.754*354.5 
F_tot_95 = 35.883*400 % million


[rno1,sno1,rnb1,snb1,rm1,sbm1,rb1,sb1, rhob1, sbw1, sow1,markup11,markup21] = Solve_local(F,sigma,psim,psiw,alpha,k,player_5(3),player_5(1),player_5(2),qn,qb,eta,rhoo);
sigma2=38;
[rno2,sno2,rnb2,snb2,rm2,sbm2,rb2,sb2, rhob2, sbw2, sow2,markup12,markup22] = Solve_local(F,sigma2,psim,psiw,alpha,k,player_5(3),player_5(1),player_5(2),qn,qb,eta,rhoo);

% warehouse lending rate 
markup1 = rhob1-psiw
markup2 = rhob2-psiw
d_rhob = rhob1-rhob2 % 54.24bps
d_rhob/rhob2 % 16.5%

% mortgage rate
ave_rn01 = (rnb1*snb1*sbw1*player_5(3)*player_5(1)+rno1*sno1*sow1*player_5(1))/(snb1*sbw1*player_5(3)*player_5(1)+sno1*sow1*player_5(1));
ave_rn1 = (rnb2*snb2*sbw2*player_5(3)*player_5(1)+rno2*sno2*sow2*player_5(1))/(snb2*sbw2*player_5(3)*player_5(1)+sno2*sow2*player_5(1));
diff1_rn = ave_rn01-ave_rn1 % 5.5bps
diff1_rn/ave_rn1 % 1.2%


% Intermediated by shadow bank
shadow_share1 = sno1*player_5(1)*sow1+snb1*player_5(1)*sbw1*player_5(3)
shadow_share2 = sno2*player_5(1)*sow2+snb2*player_5(1)*sbw2*player_5(3)
ds_volume = (shadow_share1-shadow_share2)*F % -20.8M
ds_volume/(shadow_share2*F) % -5.1%

% Intermediated by bank
bank_share1 = sb1*player_5(3)+sbm1*player_5(2)
bank_share2 = sb2*player_5(3)+sbm2*player_5(2)
db_volume = (bank_share1-bank_share2) % 39bps


% Volume
outside1 = 1-shadow_share1-bank_share1
outside2 = 1-shadow_share2-bank_share2
d_credit = (shadow_share1+bank_share1-shadow_share2-bank_share2)*F % average msa: -11.0M
(shadow_share1+bank_share1-shadow_share2-bank_share2)/(shadow_share2+bank_share2) % 0.8%

% consumer surplus 
annuity_factor = (1-(1+0.04)^(-3))/0.04
surplus1 =0.01*F*annuity_factor*( (log(player_5(3)*exp(-alpha*rb1+qb)+player_5(2)*exp(-alpha*rm1+qb)+player_5(1)*sow1*exp(-alpha*rno1+qn)+player_5(1)*sbw1*player_5(1)*exp(-alpha*rnb1+qn))/alpha))
surplus2 =0.01*F*annuity_factor*( (log(player_5(3)*exp(-alpha*rb2+qb)+player_5(2)*exp(-alpha*rm2+qb)+player_5(1)*sow2*exp(-alpha*rno2+qn)+player_5(1)*sbw2*player_5(1)*exp(-alpha*rnb2+qn))/alpha))

d_surplus = surplus1-surplus2 % -3.1M
d_surplus/(0.01*F) % -12.5


%% less concentrated 
F= 35883*354500 % potential buyers * average mortgage size 

[rno1,sno1,rnb1,snb1,rm1,sbm1,rb1,sb1, rhob1, sbw1, sow1,markup11,markup21] = Solve_local(F,sigma,psim,psiw,alpha,k,player_95(3),player_95(1),player_95(2),qn,qb,eta,rhoo);

sigma2=38;
[rno2,sno2,rnb2,snb2,rm2,sbm2,rb2,sb2, rhob2, sbw2, sow2,markup12,markup22] = Solve_local(F,sigma2,psim,psiw,alpha,k,player_95(3),player_95(1),player_95(2),qn,qb,eta,rhoo);

% warehouse lending rate 
markup1 = rhob1-psiw
markup2 = rhob2-psiw
d_rhob = rhob1-rhob2 % 30.15bps
d_rhob/rhob2 % 9.2%
% mortgage rate
ave_rn01 = (rnb1*snb1*sbw1*player_95(3)*player_95(1)+rno1*sno1*sow1*player_95(1))/(snb1*sbw1*player_95(3)*player_95(1)+sno1*sow1*player_95(1));
ave_rn1 = (rnb2*snb2*sbw2*player_95(3)*player_95(1)+rno2*sno2*sow2*player_95(1))/(snb2*sbw2*player_95(3)*player_95(1)+sno2*sow2*player_95(1));
diff1_rn = ave_rn01-ave_rn1 % 3.28bps
diff1_rn/ave_rn1 

% Intermediated by shadow bank
shadow_share1 = sno1*player_95(1)*sow1+snb1*player_95(1)*sbw1*player_95(3)
shadow_share2 = sno2*player_95(1)*sow2+snb2*player_95(1)*sbw2*player_95(3)
ds_volume = (shadow_share1-shadow_share2)*F % 109.6M
ds_volume/(shadow_share2*F)  %-2.33%

% Intermediated by bank
bank_share1 = sb1*player_95(3)+sbm1*player_95(2)
bank_share2 = sb2*player_95(3)+sbm2*player_95(2)
db_volume = (bank_share1-bank_share2)/bank_share2
100*(bank_share1-bank_share2)  %52.79bps

% Volume
outside1 = 1-shadow_share1-bank_share1
outside2 = 1-shadow_share2-bank_share2
d_credit = (shadow_share1+bank_share1-shadow_share2-bank_share2)*F % 42.4M
(shadow_share1+bank_share1-shadow_share2-bank_share2)/(shadow_share2+bank_share2) %0.44%

% consumer surplus 
annuity_factor = (1-(1+0.04)^(-10))/0.04
surplus1 =0.01*F*annuity_factor*( (log(player_95(3)*exp(-alpha*rb1+qb)+player_95(2)*exp(-alpha*rm1+qb)+player_95(1)*sow1*exp(-alpha*rno1+qn)+player_95(1)*sbw1*player_95(1)*exp(-alpha*rnb1+qn))/alpha))
surplus2 =0.01*F*annuity_factor*( (log(player_95(3)*exp(-alpha*rb2+qb)+player_95(2)*exp(-alpha*rm2+qb)+player_95(1)*sow2*exp(-alpha*rno2+qn)+player_95(1)*sbw2*player_95(1)*exp(-alpha*rnb2+qn))/alpha))

d_surplus = surplus1-surplus2 % -74.1M
d_surplus/(0.01*F)  % -0.58bps



%%
%%%%%%%% local preference for shadow bank services 
% low preference for shadow bank services 
F= 12826*354500 % potential buyers * average mortgage size 
qn1 = 0.75
[rno1,sno1,rnb1,snb1,rm1,sbm1,rb1,sb1, rhob1, sbw1, sow1,markup11,markup21] = Solve_local(F,sigma,psim,psiw,alpha,k,player_50(3),player_50(1),player_50(2),qn1,qb,eta,rhoo);

sigma2=38;
[rno2,sno2,rnb2,snb2,rm2,sbm2,rb2,sb2, rhob2, sbw2, sow2,markup12,markup22] = Solve_local(F,sigma2,psim,psiw,alpha,k,player_50(3),player_50(1),player_50(2),qn1,qb,eta,rhoo);

% warehouse lending rate 
markup1 = rhob1-psiw
markup2 = rhob2-psiw
d_rhob = rhob1-rhob2 % 18.8bpsbps
d_rhob/rhob2 % 5.7%

% mortgage rate
ave_rn01 = (rnb1*snb1*sbw1*player_50(3)*player_50(1)+rno1*sno1*sow1*player_50(1))/(snb1*sbw1*player_50(3)*player_50(1)+sno1*sow1*player_50(1));
ave_rn1 = (rnb2*snb2*sbw2*player_50(3)*player_50(1)+rno2*sno2*sow2*player_50(1))/(snb2*sbw2*player_50(3)*player_50(1)+sno2*sow2*player_50(1));
diff1_rn = ave_rn01-ave_rn1 % +2.48
diff1_rn/ave_rn1 % 0.6%

% Intermediated by shadow bank
shadow_share1 = sno1*player_50(1)*sow1+snb1*player_50(1)*sbw1*player_50(3)
shadow_share2 = sno2*player_50(1)*sow2+snb2*player_50(1)*sbw2*player_50(3)
ds_volume = (shadow_share1-shadow_share2)*F %13.9M
(shadow_share1-shadow_share2)/shadow_share2 % -2.5%
% Intermediated by bank
bank_share1 = sb1*player_50(3)+sbm1*player_50(2)
bank_share2 = sb2*player_50(3)+sbm2*player_50(2)
db_volume = (bank_share1-bank_share2) % 19bps

% Volume
outside1 = 1-shadow_share1-bank_share1
outside2 = 1-shadow_share2-bank_share2
d_credit = (shadow_share1+bank_share1-shadow_share2-bank_share2)*F % -5.9M
(shadow_share1+bank_share1-shadow_share2-bank_share2)/(shadow_share2+bank_share2) % -0.2%

% consumer surplus 
annuity_factor = (1-(1+0.04)^(-3))/0.04
surplus1 =0.01*F*annuity_factor*( (log(player_50(3)*exp(-alpha*rb1+qb)+player_50(2)*exp(-alpha*rm1+qb)+player_50(1)*sow1*exp(-alpha*rno1+qn)+player_50(1)*sbw1*player_50(1)*exp(-alpha*rnb1+qn))/alpha))
surplus2 =0.01*F*annuity_factor*( (log(player_50(3)*exp(-alpha*rb2+qb)+player_50(2)*exp(-alpha*rm2+qb)+player_50(1)*sow2*exp(-alpha*rno2+qn)+player_50(1)*sbw2*player_50(1)*exp(-alpha*rnb2+qn))/alpha))

d_surplus = surplus1-surplus2 % 6.7M
d_surplus/(0.01*F)  % 14.8bps 

%% high preference
% low preference for shadow bank services 
F= 9611*354500 % potential buyers * average mortgage size 
qn2 = 2.26
[rno1,sno1,rnb1,snb1,rm1,sbm1,rb1,sb1, rhob1, sbw1, sow1,markup11,markup21] = Solve_local(F,sigma,psim,psiw,alpha,k,player_50(3),player_50(1),player_50(2),qn2,qb,eta,rhoo);

sigma2=38;
[rno2,sno2,rnb2,snb2,rm2,sbm2,rb2,sb2, rhob2, sbw2, sow2,markup12,markup22] = Solve_local(F,sigma2,psim,psiw,alpha,k,player_50(3),player_50(1),player_50(2),qn2,qb,eta,rhoo);

% warehouse lending rate 
markup1 = rhob1-psiw
markup2 = rhob2-psiw
d_rhob = rhob1-rhob2 % 57.3
d_rhob/rhob2 % 17.4

% mortgage rate
ave_rn01 = (rnb1*snb1*sbw1*player_50(3)*player_50(1)+rno1*sno1*sow1*player_50(1))/(snb1*sbw1*player_50(3)*player_50(1)+sno1*sow1*player_50(1));
ave_rn1 = (rnb2*snb2*sbw2*player_50(3)*player_50(1)+rno2*sno2*sow2*player_50(1))/(snb2*sbw2*player_50(3)*player_50(1)+sno2*sow2*player_50(1));
diff1_rn = ave_rn01-ave_rn1 % +5.6
diff1_rn/ave_rn1 %1.3%

% Intermediated by shadow bank
shadow_share1 = sno1*player_50(1)*sow1+snb1*player_50(1)*sbw1*player_50(3)
shadow_share2 = sno2*player_50(1)*sow2+snb2*player_50(1)*sbw2*player_50(3)
ds_volume = (shadow_share1-shadow_share2)*F % -50.3M
(shadow_share1-shadow_share2)/shadow_share2 %-3.8%

% Intermediated by bank
bank_share1 = sb1*player_50(3)+sbm1*player_50(2)
bank_share2 = sb2*player_50(3)+sbm2*player_50(2)
db_volume = (bank_share1-bank_share2) %129

% Volume
outside1 = 1-shadow_share1-bank_share1
outside2 = 1-shadow_share2-bank_share2
d_credit = (shadow_share1+bank_share1-shadow_share2-bank_share2)*F % 21.4M
(shadow_share1+bank_share1-shadow_share2-bank_share2)/(shadow_share2+bank_share2)  % 84bps

% consumer surplus 
annuity_factor = (1-(1+0.04)^(-3))/0.04
surplus1 =0.01*F*annuity_factor*( (log(player_50(3)*exp(-alpha*rb1+qb)+player_50(2)*exp(-alpha*rm1+qb)+player_50(1)*sow1*exp(-alpha*rno1+qn)+player_50(1)*sbw1*player_50(1)*exp(-alpha*rnb1+qn))/alpha))
surplus2 =0.01*F*annuity_factor*( (log(player_50(3)*exp(-alpha*rb2+qb)+player_50(2)*exp(-alpha*rm2+qb)+player_50(1)*sow2*exp(-alpha*rno2+qn)+player_50(1)*sbw2*player_50(1)*exp(-alpha*rnb2+qn))/alpha))

d_surplus = surplus1-surplus2 % -7.0M
d_surplus/(0.01*F)  % -6.12bps 

